package cn.haona.game.simb.wap.servlet.init.object;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.haona.game.simb.object.god.CacheManager;
import cn.haona.game.simb.object.god.MainManager;
import cn.haona.game.simb.object.god.cache.CacheImpl_Memcached;
import cn.haona.game.simb.servlet.Util;

public class InitCache {
	//WEB应用
	final public static String TAG_SERVERS="servers";
	final public static String TAG_SERVER="server";
	final public static String ATTR_SERVER_WEIGHTS="weights";
	
	final public static String TAG_INIT_CONN_NUM="initConnNum";
	final public static String TAG_MIN_CONN_NUM="minConnNum";
	final public static String TAG_MAX_CONN_NUM="maxConnNum";
	final public static String TAG_MAX_IDLE="maxIdle";
	final public static String TAG_MAIN_T_SLEEP="mainTsleep";
	
	final public static String TAG_SOCKET_TO="socketTO";
	final public static String TAG_SOCKET_CONNECT_TO="socketConnectTO";
	
	public void initMe(String fileName)
	{
		// 字符串
		String strXML = Util.getStringFromLoadFile(fileName);

		// 解析数据
		Document document;
		try {
			document = DocumentHelper.parseText(strXML);
			// 跟元素
			Element root = document.getRootElement();
			
			String socketConnectTO=root.elementTextTrim(TAG_SOCKET_CONNECT_TO);
			String socketTO=root.elementTextTrim(TAG_SOCKET_TO);
			String mainTsleep=root.elementTextTrim(TAG_MAIN_T_SLEEP);
			String maxIdle=root.elementTextTrim(TAG_MAX_IDLE);
			String maxConnNum=root.elementTextTrim(TAG_MAX_CONN_NUM);
			
			String minConnNum=root.elementTextTrim(TAG_MIN_CONN_NUM);
			String initConnNum=root.elementTextTrim(TAG_INIT_CONN_NUM);
			
			//服务器组
			Element elementServers=root.element(TAG_SERVERS);
			//服务器
			List<Element> elementSs=elementServers.elements(TAG_SERVER);
			Iterator<Element> iteraElementSs=elementSs.iterator();
			
			//远程缓存
			CacheImpl_Memcached remoteCache=new CacheImpl_Memcached();
			remoteCache.setInitConnNum(Integer.parseInt(initConnNum));
			remoteCache.setMainTsleep(Long.parseLong(mainTsleep));
			remoteCache.setMaxConnNum(Integer.parseInt(maxConnNum));
			remoteCache.setMaxIdle(Long.parseLong(maxIdle));
			remoteCache.setMinConnNum(Integer.parseInt(minConnNum));
			remoteCache.setSocketConnectTO(Integer.parseInt(socketConnectTO));
			remoteCache.setSocketTO(Integer.parseInt(socketTO));
			
			String[] arrServers=new String[elementSs.size()];
			Integer[] arrWeights=new Integer[elementSs.size()];
			for(int i=0;i<arrServers.length;i++)
			{
				Element elementServer=elementSs.get(i);
				String ip=elementServer.getTextTrim();
				Attribute attr=elementServer.attribute(ATTR_SERVER_WEIGHTS);
				String weight=attr.getText();
				arrServers[i]=ip;
				arrWeights[i]=Integer.parseInt(weight);
				
			}
			remoteCache.setServers(arrServers);
			remoteCache.setWeights(arrWeights);
			
			remoteCache.initMe();
			
			//缓存管理器
			CacheManager cacheManager=new CacheManager();
			cacheManager.setRemoteCache(remoteCache);
			//缓存管理器放入总管理器中
			MainManager.getInstance().setCacheManager(cacheManager);
			
//			String key="4d59f0c8-cd7d-4dd5-b03c-16664eba6818";
//			remoteCache.add(key, "123");
//			String value=(String)remoteCache.get(key);
//			
//			System.out.println(">>>>>>>>>>>>>>>remoteCache="+remoteCache);
			//WEB应用root
//			String webapp_root=root.elementTextTrim(TAG_WEBAPP_ROOT);
//			WebDefine.WEB_APP_ROOT=webapp_root;
//			System.out.println("web_app_root="+webapp_root);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
